package com.qire.manhua.model.viewModel.impl;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.support.AbsBringDialogViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.dal.service.ComicReaderFinishService;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.IComicReaderFinishViewModel;

public class ComicReaderFinishViewModel extends AbsBringDialogViewModel<IComicReaderFinishViewModel> {

    private ComicReaderFinishService readerFinishService = new ComicReaderFinishService(this);

    private ComicBookEntity comicBookEntity;

    private int nextChaptersId;

    //region    #附加管理 begin
    public void onAddition() {
        refreshFavoritesBtnUI();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.readerFinishService = null;
    }
    //endregion #附加管理 end

    //region    #数据操作 begin
    public void loadData(int comicId) {
        readerFinishService.fetchComicReadFinishPush(comicId);
    }

    public void loadComicBookEntity(ComicBookEntity comicBookEntity) {
        this.comicBookEntity = comicBookEntity;
        put("pictureResourceList", comicBookEntity.currentReadingChapters().pictureResourcesList);
    }

    public void setNextChaptersId(int nextChaptersId) {
        this.nextChaptersId = nextChaptersId;
    }

    public int comicId() {
        return comicBookEntity.id;
    }

    public ComicBookEntity readNext() {
        comicBookEntity.chaptersReading(nextChaptersId);
        return comicBookEntity;
    }

    /**
     * 添加或移除收藏夹
     */
    public void whetherToFavorites() {
        if(ObjectUtil.nonNull(comicBookEntity)) {
            readerFinishService.toggleCollectionBy(comicBookEntity.id);
        }
    }

    /**
     * 刷新收藏结果
     * @param resultMsg 收藏结果信息
     * @param takeMsg 任务信息
     */
    public void refreshFavorite(String resultMsg, String takeMsg) {
        if(StringUtil.notContains(resultMsg, "失败")) {
            comicBookEntity.whetherCollect(StringUtil.notContains(resultMsg, "取消"));
        }

        refreshFavoritesBtnUI();

        ToastHelper.showSnackBar(resultMsg, Prompt.SUCCESS);

        if(StringUtil.notEmpty(takeMsg)) {
            ToastHelper.showSnackBar(takeMsg, Prompt.SUCCESS);
        }
    }
    //endregion #数据操作 end

    //region    #UI操作 begin
    public void loadComicInfo(String bookName, String pushBookTitle, String chapterName, int lastChapterNumber, String nextUpdateDate, String coverImage, int likeCount, boolean isFinish) {
        set("comicTitle", bookName);
        set("pushBookTitle", pushBookTitle);
        set("chaptersTitle", chapterName);
        set("lastUpdateInfo", "已更新至" + lastChapterNumber + "话");
        set("nextUpdateDate", nextUpdateDate);
        set("coverImage", coverImage);
        set("likeInfo", "人气值" + StringUtil.toSymbol(likeCount));

        set("comicStatusText", isFinish ? "已完结" : "连载中");
        set("comicStatusBgRid", isFinish ? R.drawable.app_comic_reader_finish_tag_red : R.drawable.app_comic_reader_finish_tag_green);
    }

    /**
     * 刷新收藏按钮UI显示
     */
    public void refreshFavoritesBtnUI() {
        boolean isFavorite = ObjectUtil.safeRun(comicBookEntity, comic -> comic.whetherCollect(), false);
        int collectBtnBgRid = isFavorite ? R.drawable.app_quick_read_button_gray : R.drawable.app_quick_read_button_yellow;
        int collectBtnIconRid = isFavorite ? R.drawable.app_quick_read_icon_collected : R.drawable.app_quick_read_icon_collection;
        put("collectBtnBgRid", collectBtnBgRid);
        put("collectBtnIconRid", collectBtnIconRid);
    }
    //endregion #UI操作 end


}
